# -*- coding: utf-8 -*-
# !/usr/bin/env python
from rest_framework.views import APIView
from components.kubernetes.dao import get_config_file_text
from utils.K8S import K8sApi
from utils.views import Result, ReturnClass

class Services(APIView):
    def get(self, request):
        data = request.query_params
        kubernetes_cluster_id = data['kubernetes_cluster_id']

        kubernetes_cluster_sign_kubeconfig_r = get_config_file_text(kubernetes_cluster_id)

        if ReturnClass.if_ok(kubernetes_cluster_sign_kubeconfig_r):
            kubernetes_cluster_sign_kubeconfig = ReturnClass.get_value(kubernetes_cluster_sign_kubeconfig_r)
            if not kubernetes_cluster_sign_kubeconfig:
                return Result.fail_response(msg="需求文件kubeconfig不存在，请在集群管理中进行配置!!!")
        else:
            return Result.fail_response(msg='查找集群API标记中的配置文件错误：（%s）' % ReturnClass.get_value(kubernetes_cluster_sign_kubeconfig_r))

        k8s_api = K8sApi(kubernetes_cluster_sign_kubeconfig)

        try:
            return_data = k8s_api.get_service()
        except Exception as e:
            return Result.fail_response(msg=str(e))

        return Result.success_response(data=return_data, msg='执行成功')